package com.gxuwz.icefrede.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.icefrede.entity.User;
import com.gxuwz.icefrede.service.UserService;
import com.gxuwz.icefrede.utils.MD5Util;
import com.gxuwz.icefrede.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 用户登录
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public User login(@RequestParam String username,@RequestParam String password, HttpSession session){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username);
        User user = userService.getOne(queryWrapper);
        if (user!=null){
            String pwd = MD5Util.MD5(password);
            //判断密码是否正确，状态是否正常
            boolean b = user.getPassword().equals(pwd);
            long status = user.getStatus();
            User users = new User();
            users.setId(user.getId());
            users.setRole(user.getRole());
            if (b && status == 1){
                //登录成功，把用户信息存储在session中
                session.setAttribute("user",users);
                //把用户信息存储在redis中，有效期为1小时
                TimeUnit unit = TimeUnit.SECONDS;
                // 我们使用用户的 ID 作为 Redis 的 key
                redisUtil.set("user:" + user.getId(), user, 60 * 60 ,unit); // 存储 1 小时
                return users;
            }
        }
        //登录失败
        return null;
    }

    /**
     * 得到当前登录用户信息
     * @param session
     * @return
     */
    @RequestMapping("/get_role")
    public String getCurrentUser(HttpSession session){
        if (session.getAttribute("user") == null){
            return null;
        }
        User user = (User) session.getAttribute("user");
        return user.getRole();
    }

    /**
     * 用户退出登录
     * @param session
     * @return
     */

    @RequestMapping("/login_out")
    public String loginOut(HttpSession session){
        //删除存储在session的userID
        User user = (User) session.getAttribute("user");
        session.removeAttribute("user");
        redisUtil.delete("user:"+user.getId());
        return "退出成功";
    }

    /**
     * 查询所有用户
     * 分页查询
     * @return
     */
    @RequestMapping("/user_list")
    public Page<User> userList(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        //新建分页构造函数Page<T> T为目标实体类
        Page<User> userPage = new Page<User>(pageNum, pageSize);
        //result为分页查询结果
        Page<User> result = userService.page(userPage);
        return result;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public String deleteUser(int id){
        boolean b = userService.removeById(id);
        if (b){
            return "删除成功";
        }
        return null;
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @RequestMapping("/update")
    public boolean updateUser(@RequestBody User user){
        return userService.updateById(user);
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @RequestMapping("/add")
    public String addUser(@RequestBody User user){
        //密码加密
        user.setPassword(MD5Util.MD5(user.getPassword()));
        //状态默认为1
        user.setStatus(1);
        boolean b =userService.save(user);
        if (b){
            return user.getUsername();
        }
        return null;
    }


    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @RequestMapping("/get_user")
    public User getUserById(@RequestParam int id){
        return userService.getById(id);
    }


    /**
     * 模糊查询用户名
     * @param username
     * @return
     */
    @RequestMapping("/search_user")
    public Page<User> searchUser(@RequestParam String username, @RequestParam Integer pageNum, @RequestParam Integer pageSize){
        Page<User> userPage = new Page<User>(pageNum, pageSize);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(User::getUsername,username);
        return userService.page(userPage,queryWrapper);
    }


}
